﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shell;
using SABnzbdNET.Commands;
using SABnzbdNET.Events;
using SABnzbdNET.Model;
using SABnzbdNET.ServiceAgents;

namespace SABnzbdNET.ViewModels
{
    public class ServerHistoryViewModel : BaseViewModel
    {
        #region Fields and properties

        private DelegateCommand launchBrowserCommand;
        private DelegateCommand<Model.FinishedJob> deleteCommand;

        private readonly IEventAggregator eventAggregator;
        private ServerHistory serverHistory;
        private FinishedJob firstJob;

        public ICommand LaunchBrowserCommand
        {
            get { return launchBrowserCommand ?? (launchBrowserCommand = new DelegateCommand(LaunchBrowser)); }
        }
        public ICommand DeleteCommand
        {
            get { return deleteCommand ?? (deleteCommand = new DelegateCommand<Model.FinishedJob>(DeleteJob)); }
        }

        public ServerHistory ServerHistory
        {
            get { return serverHistory; }
            set
            {
                serverHistory = value;
                OnPropertyChanged("ServerHistory");
            }
        }
        public FinishedJob FirstJob
        {
            get { return firstJob; }
            set
            {
                firstJob = value;
                OnPropertyChanged("FirstJob");
            }
        }

        #endregion

        public ServerHistoryViewModel()
        {
            eventAggregator = EventAggregatorProvider.Instance;
            eventAggregator.Subscribe<UpdateServerHistoryMessage>(message => GetHistory());
        }

        private void GetHistory()
        {
            Mouse.OverrideCursor = Cursors.AppStarting;
            SetStatusMessage("Updating", "Updating");
            new ServerAgent().GetHistory(DeployResult);
        }

        private void DeployResult(ServerHistory history, string errorMessage)
        {
            if (history == null)
            {
                SetStatusMessage("Error updating history: " + errorMessage, string.Empty);
                FirstJob = null;
            }
            else
            {
                ServerHistory = history;
                if(ServerHistory.FinishedJobs != null && ServerHistory.FinishedJobs.Count > 0)
                    FirstJob = ServerHistory.FinishedJobs.FirstOrDefault();
                SetStatusMessage("Last updated " + DateTime.Now, string.Empty);
            }
            Mouse.OverrideCursor = null;
        }

        private void SetStatusMessage(string statusMessage, string notifyIconTooltip)
        {
            eventAggregator.Publish(new UpdateStatusInfoMessage
            {
                StatusMessage = statusMessage,
                NotifyIconMessage = notifyIconTooltip
            });
        }

        private static void LaunchBrowser()
        {
            Process.Start(new ProcessStartInfo(Properties.Settings.Default.ServerPath));
        }

        private void DeleteJob(Model.FinishedJob job)
        {
            new ServerAgent().DeleteFinishedDownload(job.Id, GetHistory, ShowError);
        }

        private static void ShowError(string errorMessage)
        {
            MessageBox.Show(errorMessage, "Error");
        }
    }
}
